<?php
class AddressAction extends HomeAction {

    public function index(){ 
    	$map['memberID'] = $this->_user['memberID'];
        $list = M('Address')->where($map)->select();
        $this->assign('list',$list);
        $this->display();       
    }

    public function save(){
        $obj = D('Address');
        if ($data = $obj->create()) {
            if ($list = $obj->add($data)) {
                $state = 'SUCCESS';
                $message = '操作成功';
                $url = U('Store/order',array('addID'=>$list));
            } else {
                $state = '操作失败';
            }
        } else {
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state,$message,$url); 
    }   

    public function setDefault(){
        $map['memberID'] = $this->_user['memberID'];
        M('Address')->where($map)->setField('def',0);

        $id = I('get.id');
        $map['id']=$id;
        M('Address')->where($map)->setField('def',1);
        redirect ($_SERVER['HTTP_REFERER']);
    }   

    public function del(){
        $id = I('get.id');
        $map['id']=$id;
        $map['memberID'] = $this->_user['memberID'];
        M('Address')->where($map)->delete();
        redirect ($_SERVER['HTTP_REFERER']);
    }
}